####################
# Case 1 (L) #######
####################
#Load data (Update source)
#DiDdata <- read_dta("/Users/k1466170/Dropbox/Swing riots/petitions/R code/did_v2_tjbal2.dta")
DiDdata <- read_dta("/Users/k1466170/Dropbox/Swing riots/petitions/R code/did.dta")
head(DiDdata)
class(DiDdata)
DiDdata<-DiDdata[!(DiDdata$new==1 | DiDdata$random==1),]
DiDdata<-DiDdata[DiDdata$sch_A==1 | DiDdata$sch_B==1 | !is.na(DiDdata$drummond),]
#DiDdata<-DiDdata[DiDdata$sch_A==1 | !is.na(DiDdata$drummond),]
#DiDdata<-DiDdata[!(DiDdata$sch_B==1),,]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# | DiDdata$year==1832
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$sch_A == 1] <- 1
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
#panelView(ln_petitions ~ treated +ln_pop_i, data=DiDdata, show.id=c(1:50), index=c("ID","year"), xlab="Year", ylab="constituency", by.timing=TRUE)
########
#DID ###
########
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), Y.match.npre=0, demean=TRUE, vce="fixed.weights", nsims=200)
print(out.did)
plot(out.did, ylab="ATT", ylim=c(-0.5,0.5))
plot(out.did, type="counterfactual", count=FALSE)
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean = FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="/Users/k1466170/Dropbox/Swing riots/petitions/R code/Case1_mean.jpeg")
plot(out.did, ylab="ATT", ylim=c(-1,0.5))
dev.off()
plot(out.did, ylab="ATT", ylim=c(-1,0.5))
plot(out.did, type="ct", ylab="ATT", ylim=c(-2,2))
jpeg(filename="/Users/k1466170/Dropbox/Swing riots/petitions/R code/Case1_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
plot(out.did, type = "balance")
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean=FALSE, estimator="meanfirst", vce="jackknife")
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean=FALSE, estimator="meanfirst", vce="jackknife")
plot(out.did, ylab="ATT", ylim=c(-1,0.5))
print(out.did)
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean=FALSE, estimator="meanfirst", vce="jackknife")
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean = FALSE, estimator="mean", vce="jackknife")
print(out.did)
plot(out.did, ylab="ATT", ylim=c(-1,0.5))
####################
# Case 2 (G) #######
####################
#Load data
#DiDdata <- read_dta("/Users/k1466170/Dropbox/Swing riots/petitions/R code/did_v2_tjbal2.dta")
DiDdata <- read_dta("/Users/k1466170/Dropbox/Swing riots/petitions/R code/did.dta")
head(DiDdata)
class(DiDdata)
DiDdata<-DiDdata[!(DiDdata$sch_A == 1 | DiDdata$sch_B == 1 | DiDdata$random == 1),]
DiDdata<-DiDdata[is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
#| DiDdata$year==1832
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
#gen treatgroup = 0
#replace treatgroup =  1 if sch_A==1 | sch_B == 1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$new == 1] <- 1
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), Y.match.npre=0, demean=TRUE, vce="fixed.weights", nsims=200)
print(out.did)
plot(out.did, ylab="ATT", ylim=c(-1,1))
plot(out.did, type="counterfactual", count=FALSE)
plot(out.did, ylab="ATT", ylim=c(-1,1))
plot(out.did, type="counterfactual", count=FALSE)
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
plot(out.did, type="ct", ylab="ATT", ylim=c(-2,2))
plot(out.did, type="ct", ylab="ATT", ylim=c(-0.5,3))
plot(out.did, type="counterfactual", count=FALSE)
begin.time<-Sys.time()
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), estimator="meanfirst", demean=FALSE, vce="jackknife")
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
plot(out.did, type="ct", ylab="ATT", ylim=c(-2,2))
plot(out.did, type="counterfactual", count=FALSE)
plot(out.did, type = "balance")
rm(list=ls())
setwd(getwd())
#library(devtools)
#library(foreign)
#library(remote)
#library(coefplot)
#library(xtable)
#library(texreg)
#library(kbal)
#library(tjbal)
#library(panelView)
#library(future)
#library(haven)
#########################################
##### TJBAL ########################
#########################################
####################
# Case 1 (L) #######
####################
# Load data
DiDdata <- read_dta("/Users/k1466170/Dropbox/Swing riots/petitions/R code/did.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$new==1 | DiDdata$random==1),]
DiDdata<-DiDdata[DiDdata$sch_A==1 | DiDdata$sch_B==1 | !is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$sch_A == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean = FALSE, estimator="mean", vce="jackknife")
print(out.did)
plot(out.did, ylab="ATT", ylim=c(-1,0.5))
rm(list=ls())
setwd(getwd())
#library(devtools)
#library(foreign)
#library(remote)
#library(coefplot)
#library(xtable)
#library(texreg)
#library(kbal)
#library(tjbal)
#library(panelView)
#library(future)
#library(haven)
DiDdata <- read_dta("/Users/k1466170/Dropbox/Swing riots/petitions/R code/did.dta")
DiDdata<-DiDdata[!(DiDdata$sch_A == 1 | DiDdata$sch_B == 1 | DiDdata$random == 1),]
DiDdata<-DiDdata[is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
#| DiDdata$year==1832
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
#gen treatgroup = 0
#replace treatgroup =  1 if sch_A==1 | sch_B == 1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$new == 1] <- 1
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), Y.match.npre=0, demean=TRUE, vce="fixed.weights", nsims=200)
print(out.did)
plot(out.did, ylab="ATT", ylim=c(-1,1))
plot(out.did, type="counterfactual", count=FALSE)
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
plot(out.did, type="counterfactual", count=FALSE)
begin.time<-Sys.time()
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), estimator="meanfirst", demean=FALSE, vce="jackknife")
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="/Users/k1466170/Dropbox/Swing riots/petitions/R code/CaseG_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
plot(out.did, type="counterfactual", count=FALSE)
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean = FALSE, estimator="mean", vce="jackknife")
print(out.did)
plot(out.did, ylab="ATT", ylim=c(-1,0.5))
plot(out.did, ylab="ATT", ylim=c(-1,2.5))
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
plot(out.did, ylab="ATT", ylim=c(-1,0.5))
plot(out.did, type="ct", ylab="ATT", ylim=c(-2,2))
plot(out.did, type="counterfactual", count=FALSE)
rm(list=ls())
setwd(getwd())
DiDdata <- read_dta("../dta/did_rep.dta")
getwd()
install.packages("here")   # run once
library(here)
getwd()
here::here()
getwd()
rm(list=ls())
setwd(getwd())
#library(devtools)
#library(foreign)
#library(remote)
#library(coefplot)
#library(xtable)
#library(texreg)
#library(kbal)
#library(tjbal)
#library(panelView)
#library(future)
#library(haven)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
getwd()
DiDdata <- read_dta("../dta/did_rep.dta")
DiDdata<-DiDdata[!(DiDdata$sch_A == 1 | DiDdata$sch_B == 1 | DiDdata$random == 1),]
DiDdata<-DiDdata[!(DiDdata$sch_A == 1 | DiDdata$sch_B == 1),]
DiDdata<-DiDdata[is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$new == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
plot(out.did, type="counterfactual", count=FALSE)
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
DiDdata <- read_dta("../dta/did_rep.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$new==1),]
DiDdata<-DiDdata[DiDdata$sch_A==1 | DiDdata$sch_B==1 | !is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$sch_A == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean = FALSE, estimator="mean", vce="jackknife")
print(out.did)
plot(out.did, type="counterfactual", count=FALSE)
plot(out.did, type="counterfactual", count=FALSE)
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
rm(list=ls())
setwd(getwd())
#library(devtools)
#library(foreign)
#library(remote)
#library(coefplot)
#library(xtable)
#library(texreg)
#library(kbal)
#library(tjbal)
#library(panelView)
#library(future)
#library(haven)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#########################################
##### TJBAL ########################
#########################################
############
# Case T=G #
############
#Load data
DiDdata <- read_dta("../dta/did_rep.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$sch_A == 1 | DiDdata$sch_B == 1),]
DiDdata<-DiDdata[is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$new == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="/Users/k1466170/Dropbox/Swing riots/petitions/R code/CaseG_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
jpeg(filename="/Users/k1466170/Dropbox/Swing riots/petitions/R code/CaseG_mean.jpeg")
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
dev.off()
############
# Case T=L #
############
# Load data
DiDdata <- read_dta("../dta/did_rep.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$new==1),]
DiDdata<-DiDdata[DiDdata$sch_A==1 | DiDdata$sch_B==1 | !is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$sch_A == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean = FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="/Users/k1466170/Dropbox/Swing riots/petitions/R code/CaseL_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
jpeg(filename="/Users/k1466170/Dropbox/Swing riots/petitions/R code/CaseL_mean.jpeg")
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
dev.off()
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename=".../CaseG_mean_trend.jpeg")
jpeg(filename="CaseG_mean_trend.jpeg")
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="CaseG_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
rm(list=ls())
setwd(getwd())
#library(devtools)
#library(foreign)
#library(remote)
#library(coefplot)
#library(xtable)
#library(texreg)
#library(kbal)
#library(tjbal)
#library(panelView)
#library(future)
#library(haven)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#########################################
##### TJBAL ########################
#########################################
############
# Case T=G #
############
#Load data
DiDdata <- read_dta("../dta/did_rep.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$sch_A == 1 | DiDdata$sch_B == 1),]
DiDdata<-DiDdata[is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$new == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="CaseG_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
jpeg(filename="CaseG_mean.jpeg")
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
dev.off()
############
# Case T=L #
############
# Load data
DiDdata <- read_dta("../dta/did_rep.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$new==1),]
DiDdata<-DiDdata[DiDdata$sch_A==1 | DiDdata$sch_B==1 | !is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$sch_A == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean = FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="CaseL_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
jpeg(filename="CaseL_mean.jpeg")
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
dev.off()
# This was run using a Macbook Pro with an Apple M1 Pro chip, using R version 4.2.2 through RStudio
# Requires the installation of tjbal (https://yiqingxu.org/packages/tjbal/tjbal.html).
rm(list=ls())
setwd(getwd())
#library(devtools)
#library(foreign)
#library(remote)
#library(coefplot)
#library(xtable)
#library(texreg)
#library(kbal)
#library(tjbal)
#library(panelView)
#library(future)
#library(haven)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#########################################
##### TJBAL ########################
#########################################
############
# Case T=G #
############
#Load data
DiDdata <- read_dta("../dta/did_rep.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$sch_A == 1 | DiDdata$sch_B == 1),]
DiDdata<-DiDdata[is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$new == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="CaseG_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
jpeg(filename="CaseG_mean.jpeg")
plot(out.did, ylab="ATT", ylim=c(-1.5,1))
dev.off()
############
# Case T=L #
############
# Load data
DiDdata <- read_dta("../dta/did_rep.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$new==1),]
DiDdata<-DiDdata[DiDdata$sch_A==1 | DiDdata$sch_B==1 | !is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$sch_A == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata, index=c("ID","year"), demean = FALSE, estimator="mean", vce="jackknife")
print(out.did)
jpeg(filename="CaseL_mean_trend.jpeg")
plot(out.did, type="counterfactual", count=FALSE)
dev.off()
jpeg(filename="CaseL_mean.jpeg")
plot(out.did, ylab="ATT", ylim=c(-1,0.5))
dev.off()
# This was run using a Macbook Pro with an Apple M1 Pro chip, using R version 4.2.2
# Requires the installation of tjbal (https://yiqingxu.org/packages/tjbal/tjbal.html).
rm(list=ls())
setwd(getwd())
#library(devtools)
#library(foreign)
#library(remote)
#library(coefplot)
#library(xtable)
#library(texreg)
#library(kbal)
#library(tjbal)
#library(panelView)
#library(future)
#library(haven)
#########################################
##### TJBAL ########################
#########################################
############
# Case T=G #
############
#Load data
DiDdata <- read_dta("/Users/k1466170/Dropbox/Swing riots/petitions/R code/did.dta")
# Subset the data
DiDdata<-DiDdata[!(DiDdata$sch_A == 1 | DiDdata$sch_B == 1 | DiDdata$random == 1),]
DiDdata<-DiDdata[is.na(DiDdata$drummond),]
DiDdata<-DiDdata[!(DiDdata$year==1830 | DiDdata$year==1831 | DiDdata$year==1832),]
# Define variables
DiDdata$post<-0
DiDdata$post[DiDdata$year>1832] <-1
DiDdata$treatgroup<-0
DiDdata$treatgroup[DiDdata$new == 1] <- 1
# Define treatment
DiDdata$treated <- DiDdata$post * DiDdata$treatgroup
##################
# Mean balancing #
##################
out.did <- tjbal(ln_petitions ~ treated, data=DiDdata,
index=c("ID","year"), demean=FALSE, estimator="mean", vce="jackknife")
print(out.did)
library(tjbal)
